package cn.wwcyc.controller;

import cn.wwcyc.domain.*;
import cn.wwcyc.mapper.TjBjXxMapper;
import cn.wwcyc.service.TjBjXxService;
import cn.wwcyc.service.TjDwService;
import cn.wwcyc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/bj")
public class BJController {
    @Autowired
    private UserService userService;
    @Autowired
    private TjDwService tjDwService;
    @Autowired
    private TjBjXxMapper bjXxMapper;
    @Autowired
    TjBjXxService bjXxService;
    //查询所有的报警
    @RequestMapping("/getpage")
    public R<IPage<TjBjXxVo>> getPage(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long dw,
            @RequestParam(required = false) String bjd,
            @RequestParam(required = false) String bjlx,
            @RequestParam(required = false) String msxx,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startdate,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date enddate,
            HttpServletRequest request
    ) {
        //TODO
        IPage<TjBjXxVo> page = new Page<>(pagenum, pagesize);
        QueryWrapper<TjBjXx> qw = new QueryWrapper<>();
        if (!StringUtils.isBlank(bjd)) {
            qw.like("bjd", bjd);
        }
        if (!StringUtils.isBlank(bjlx)) {
            qw.eq("lx", bjlx);
        }
        if (!StringUtils.isBlank(msxx)) {
            qw.like("MSXX", msxx);//oracle使用双引号进行转义，desc在oracle中是关键字，需要转义
        }
        if (startdate != null) {
            qw.ge("bjsj", startdate);
        }
        if (enddate != null) {
            qw.le("bjsj", enddate);
        }
        User user = userService.getCurrentUser(request);
        if(dw == null)
            dw = user.getDw();
        TjDw tjDw = null;
        if (dw != null) {
            tjDw = tjDwService.getDwTreeById(dw);
        }
        List<TjDw> dwlist = new ArrayList<>();
        dwlist = tjDwService.DwTreeToList(dwlist, tjDw);
        List<Long> dwIdList = new ArrayList<>();
        for (TjDw tjDw1 : dwlist) {
            dwIdList.add(tjDw1.getId());
        }
        if( dwIdList.size() > 0 ){
            qw.in("dw",dwIdList);
        }
        long count = bjXxMapper.selectCount(qw);
        page.setTotal(count);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            page.setCurrent(1);
        }
        List<TjBjXxVo> tjBjXxVoList = bjXxMapper.selectVoPage( page.getCurrent(), page.getSize(), dwIdList, bjd, bjlx, msxx, startdate, enddate);
        page.setRecords(tjBjXxVoList);
        return R.success(page);
    }

    //查询新的未确认的报警
    @RequestMapping("/getnewpage")
    public R<IPage<TjBjXxVo>> getNewPage(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) Long dw,
            @RequestParam(required = false) String bjd,
            @RequestParam(required = false) String bjlx,
            @RequestParam(required = false) String msxx,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startdate,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date enddate,
            HttpServletRequest request
    ) {
        //TODO
        IPage<TjBjXxVo> page = new Page<>(pagenum, pagesize);
        QueryWrapper<TjBjXx> qw = new QueryWrapper<>();
        if (!StringUtils.isBlank(bjd)) {
            qw.like("bjd", bjd);
        }
        if (!StringUtils.isBlank(bjlx)) {
            qw.eq("lx", bjlx);
        }
        if (!StringUtils.isBlank(msxx)) {
            qw.like("MSXX", msxx);//oracle使用双引号进行转义，desc在oracle中是关键字，需要转义
        }
        if (startdate != null) {
            qw.ge("bjsj", startdate);
        }
        if (enddate != null) {
            qw.le("bjsj", enddate);
        }
        User user = userService.getCurrentUser(request);
        if(dw == null)
            dw = user.getDw();
        TjDw tjDw = null;
        if (dw != null) {
            tjDw = tjDwService.getDwTreeById(dw);
        }
        List<TjDw> dwlist = new ArrayList<>();
        dwlist = tjDwService.DwTreeToList(dwlist, tjDw);
        List<Long> dwIdList = new ArrayList<>();
        for (TjDw tjDw1 : dwlist) {
            dwIdList.add(tjDw1.getId());
        }
        if( dwIdList.size() > 0 ){
            qw.in("dw",dwIdList);
        }
        qw.isNull("qrsj");
        long count = bjXxMapper.selectCount(qw);
        page.setTotal(count);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            page.setCurrent(1);
        }
        List<TjBjXxVo> tjBjXxVoList = bjXxMapper.selectNewVoPage( page.getCurrent(), page.getSize(), dwIdList, bjd, bjlx, msxx, startdate, enddate);
        page.setRecords(tjBjXxVoList);
        return R.success(page);
    }
    //查询新的未处置的报警
    @RequestMapping("/getczpage")
    public R<IPage<TjBjXxVo>> getCZPage(
            @RequestParam
            Integer pagenum,
            @RequestParam
            Integer pagesize,
            @RequestParam(required = false)
            Long dw,
            @RequestParam(required = false)
            String bjd,
            @RequestParam(required = false)
            String bjlx,
            @RequestParam(required = false)
            String msxx,
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            Date startdate,
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            Date enddate,
            HttpServletRequest request
    ) {
        //TODO
        IPage<TjBjXxVo> page = new Page<>(pagenum, pagesize);
        QueryWrapper<TjBjXx> qw = new QueryWrapper<>();
        if (!StringUtils.isBlank(bjd)) {
            qw.like("bjd", bjd);
        }
        if (!StringUtils.isBlank(bjlx)) {
            qw.eq("lx", bjlx);
        }
        if (!StringUtils.isBlank(msxx)) {
            qw.like("MSXX", msxx);//oracle使用双引号进行转义，desc在oracle中是关键字，需要转义
        }
        if (startdate != null) {
            qw.ge("bjsj", startdate);
        }
        if (enddate != null) {
            qw.le("bjsj", enddate);
        }
        User user = userService.getCurrentUser(request);
        if(dw == null)
            dw = user.getDw();
        TjDw tjDw = null;
        if (dw != null) {
            tjDw = tjDwService.getDwTreeById(dw);
        }
        List<TjDw> dwlist = new ArrayList<>();
        dwlist = tjDwService.DwTreeToList(dwlist, tjDw);
        List<Long> dwIdList = new ArrayList<>();
        for (TjDw tjDw1 : dwlist) {
            dwIdList.add(tjDw1.getId());
        }
        if( dwIdList.size() > 0 ){
            qw.in("dw",dwIdList);
        }
        qw.isNotNull("qrsj");
        qw.isNull("clsj");
        long count = bjXxMapper.selectCount(qw);
        page.setTotal(count);
        page.setPages(page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1);
        if (pagenum > page.getPages()) {
            page.setCurrent(page.getPages());
        }
        if (pagenum < 1) {
            page.setCurrent(1);
        }
        List<TjBjXxVo> tjBjXxVoList = bjXxMapper.selectCZVoPage( page.getCurrent(), page.getSize(), dwIdList, bjd, bjlx, msxx, startdate, enddate);
        page.setRecords(tjBjXxVoList);
        return R.success(page);
    }

    @RequestMapping("/qr")
    public R qr(
            @RequestParam(required = true) Long id,
            HttpServletRequest request
    ) {
        User user = userService.getCurrentUser(request);
        Long userid = user.getId();
        bjXxService.qr(id, userid);
        return R.success();
    }
    @RequestMapping("/cz")
    public R cz(
            @RequestBody CZFormDTO czform,
            HttpServletRequest request
    ) {
        User user = userService.getCurrentUser(request);
        Long userid = user.getId();
        bjXxService.cz(userid, czform);
        return R.success();
    }
}
